<?php
include('./include/session_verify.php');
include('../include/db.config.php');
function find_result($id,$page,$page_size){
	$result = array();
	$r = array();
	$page_begin = ($page-1)*$page_size;
	if($id ==1){
		$sql = "select * from challenge order by id desc limit ".$page_begin.",".$page_size;
	}elseif ($id==2) {
		$sql = "SELECT a.* ,b.id as member_id,b.name as member_name,b.team_id FROM team as a,member as b WHERE (a.id=b.team_id) and a.status=1 order by id desc limit ".$page_begin.",".$page_size;
	}elseif ($id==3) {
		$sql = "SELECT b.* ,a.name as team_name FROM team as a,member as b WHERE (a.id=b.team_id) and a.status=1 order by id desc limit ".$page_begin.",".$page_size;
	}elseif ($id ==4) {
		$sql = "select a.*,b.name as member_name,b.id as member_id,b.student_id from team as a,member as b where  a.id=b.team_id and status=0 order by id desc limit ".$page_begin.",".$page_size;
	}elseif ($id ==5) {
		$sql = "select * from challenge_radio  order by id desc limit ".$page_begin.",".$page_size;
	}
	$result = getAll($sql);
	if($id ==2||$id==3||$id==4){
		foreach ($result as $key => $value) {
			$r[$value['id']][] = $value;
		}
		//print_r($r);
		echo json_encode($r);
		die();
	}
	//print_r($result);
	echo  json_encode($result);
}
function page_number($number,$page_size){
	$tmp = $number%$page_size;
	if($tmp > 0){
		$number = (int)($number/$page_size)+1;
		return $number;
	}else{
		$number = $number/$page_size;
		return $number;
	}
}


@$page = $_GET['page'];
@$page_size = $_GET['page_size'];
@$id = $_GET['id'];
@$pagetotal = $_GET['pagetotal'];
if(isset($pagetotal)){
	$sql_challenge = "select * from challenge";
	$result_challenge = getAll($sql_challenge);
	$pagetotal_challenge = count($result_challenge );
	$sql_team = "select * from team where status='1'";
	$result_team = getAll($sql_team);
	$pagetotal_team = count($result_team);
	$sql_member = "select * from member";
	$result_member = getAll($sql_member);
	$pagetotal_member = count($result_member);
	$sql_verify = "select * from team where status='0'";
	$result_verify = getAll($sql_verify);
	$pagetotal_verify = count($result_verify);
	$sql_choice = "select * from challenge_radio";
	$result_choice = getAll($sql_choice);
	$pagetotal_choice = count($result_choice);

	$pagetotals[0] = $pagetotal_challenge;
	$pagetotals[1] = $pagetotal_team;
	$pagetotals[2] = $pagetotal_member;
	$pagetotals[3] = $pagetotal_verify;
	$pagetotals[4] = $pagetotal_choice;
	$pagetotals[0] = page_number($pagetotals[0],$page_size);
	$pagetotals[1] = page_number($pagetotals[1],$page_size);
	$pagetotals[2] = page_number($pagetotals[2],$page_size);
	$pagetotals[3] = page_number($pagetotals[3],$page_size);
	$pagetotals[4] = page_number($pagetotals[4],$page_size);
	echo json_encode($pagetotals);
}else{
find_result($id,$page,$page_size);
}